package com.dragon.mmochat.service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.MmochatMain;
import com.dragon.mmochat.message.Mmochat_17_2C;
import com.dragon.mmochat.message.Mmochat_17_3C;
import com.dragon.mmochat.message.Mmochat_17_4C;
import com.dragon.mmochat.message.Mmochat_17_5S;
import com.dragon.mmochat.model.bet.MmochatBet;
import com.dragon.mmochat.model.bet.MmochatBetInfo;
import com.dragon.mmochat.model.enumType.MmochatBetState;
import com.dragon.mmochat.model.enumType.MmochatBetType;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.util.MmochatUtil;
import com.skymobi.handler.SkymobiHandler;
import com.skymobi.message.SkymobiProtocolMessage;
import com.skymobi.service.SkymobiService;

public class MmochatBetService {
	private static Logger log = LoggerFactory
			.getLogger(MmochatBetService.class);
	public static Map<MmochatBetType, MmochatBet> bets = new ConcurrentHashMap<MmochatBetType, MmochatBet>();

	public MmochatBetService() {
		if (!MmochatMain.openMergeServer) {
			MmochatBet smallMoneyBet = new MmochatBet(MmochatBetType.游戏币赌场, 10);
			smallMoneyBet.setBetSetMoneyTime(290);
			bets.put(MmochatBetType.游戏币赌场, smallMoneyBet);

			MmochatBet bigMoneyBet = new MmochatBet(MmochatBetType.元宝赌场,
					10 + 150);
			bets.put(MmochatBetType.元宝赌场, bigMoneyBet);
		}
	}

	// 将玩家从所有赌场中移除
	public static void removePlayerFromBet(int roleId) {
		for (MmochatBet bet : bets.values()) {
			bet.getPlayersInBet().remove(roleId);
		}
	}

	// 关闭赌盘界面
	@SkymobiService
	public SkymobiProtocolMessage handleC_17_2(SkymobiHandler handler,
			Mmochat_17_2C req) {
		MmochatBetType type = MmochatBetType.get(req.getBetType());
		if (type == null) {
			log.error("不存在的赌场类型:key=" + req.getBetType());
			return null;
		}
		MmochatBet bet = bets.get(type);
		if (bet == null) {
			log.error("不存在的赌场类型:key=" + req.getBetType());
			return null;
		}
		// 从赌盘玩家列表中删除
		bet.getPlayersInBet().remove(req.getRoleId());
		return null;
	}

	// 上传压注
	@SkymobiService
	public SkymobiProtocolMessage handleC_17_3(SkymobiHandler handler,
			Mmochat_17_3C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			MmochatUtil.sendCommonMsgbox(me, "服务器忙,请稍候再试!");
			return null;
		}
		if (me.getLevel() < MmochatConstant.minLevelNeedToPlayerBet) {
			MmochatUtil.sendCommonMsgbox(me,
					MmochatConstant.minLevelNeedToPlayerBet
							+ "级前不能玩宠物赛跑，快去升级吧!");
			return null;
		}
		MmochatBetType type = MmochatBetType.get(req.getBetType());
		if (type == null) {
			MmochatUtil.sendCommonMsgbox(me, "暂未开放!");
			log.error("不存在的赌场类型:key=" + req.getBetType());
			return null;
		}
		MmochatBet bet = bets.get(type);
		if (bet == null) {
			MmochatUtil.sendCommonMsgbox(me, "暂未开放!");
			log.error("不存在的赌场类型:key=" + req.getBetType());
			return null;
		}
		if (MmochatNpcService.checkStorePwd(me)) {
			return null;
		}
		if (bet.getBetRoomState() != MmochatBetState.可以下注状态) {
			MmochatUtil.sendCommonMsgbox(me, "现在不是下注时间，下注失败!");
			return null;
		}
		MmochatBetInfo newBet;
		Integer oldSetMoney = 0;
		newBet = bet.getBetMap().get(me.getRoleId());
		if (newBet == null) {
			if (bet.getBetMap().size() >= MmochatConstant.maxPlayerNumToPlayerBet) {
				MmochatUtil.sendCommonMsgbox(me, "压注人数已达上限!请下场再压。");
				return null;
			}
			newBet = new MmochatBetInfo();
		}
		newBet.setRoleId(me.getRoleId());
		oldSetMoney = newBet.getBetMoneyMap().get(req.getIndex());
		if (oldSetMoney == null) {
			oldSetMoney = 0;
		}
		if (req.getSetMoney() < 0) {
			MmochatUtil.sendCommonMsgbox(me, "金额不能小于0!");
			return null;
		}

		newBet.getBetMoneyMap().put(req.getIndex(), req.getSetMoney());

		if (bet.getBetType() == MmochatBetType.游戏币赌场) {
			long maxSet = Math.max(MmochatConstant.maxSmallMoneyToPlayBet, me
					.getSmall_money() / 10);
			if (newBet.getTotalBetMoney() > maxSet) {
				newBet.getBetMoneyMap().put(req.getIndex(), oldSetMoney);
				Mmochat_17_5S pack = new Mmochat_17_5S();
				pack.setBet(bet);
				pack.setMe(me);
				pack.setSystemMsg(bet.getBetSystemMsg());
				MmochatUtil.sendCommonPack(me, pack);

				MmochatUtil.sendCommonMsgbox(me, "您的总压注金额不能超过"
						+ MmochatUtil.getColorMoney(maxSet)
						+ "文钱(上限为包裹金钱的十分之一。若此上限不足"
						+ MmochatConstant.maxSmallMoneyToPlayBet + "文则上限为"
						+ MmochatConstant.maxSmallMoneyToPlayBet + "文)!");
				return null;
			}
		} else if (bet.getBetType() == MmochatBetType.元宝赌场) {
			if (newBet.getTotalBetMoney() > me.getBig_money()
					- MmochatConstant.minBigMoneyRestToPlayBet) {
				newBet.getBetMoneyMap().put(req.getIndex(), oldSetMoney);
				Mmochat_17_5S pack = new Mmochat_17_5S();
				pack.setBet(bet);
				pack.setMe(me);
				pack.setSystemMsg(bet.getBetSystemMsg());
				MmochatUtil.sendCommonPack(me, pack);

				MmochatUtil
						.sendCommonMsgbox(
								me,
								"您必须保留"
										+ MmochatUtil
												.getColorMoney(MmochatConstant.minBigMoneyRestToPlayBet)
										+ "元宝以备不时之需，多余的元宝才可以下注!");
				return null;
			}

			if (req.getSetMoney() > MmochatConstant.maxBigMoneyToPlayBet) {
				newBet.getBetMoneyMap().put(req.getIndex(), oldSetMoney);
				Mmochat_17_5S pack = new Mmochat_17_5S();
				pack.setBet(bet);
				pack.setMe(me);
				pack.setSystemMsg(bet.getBetSystemMsg());
				MmochatUtil.sendCommonPack(me, pack);

				MmochatUtil
						.sendCommonMsgbox(
								me,
								"元宝不能大于"
										+ MmochatUtil
												.getColorMoney(MmochatConstant.maxBigMoneyToPlayBet)
										+ "元宝。");
				return null;
			}
		}

		if (newBet.getTotalBetMoney() <= 0) {
			bet.getBetMap().remove(me.getRoleId());
		} else {
			bet.getBetMap().put(me.getRoleId(), newBet);
		}

		Mmochat_17_5S pack = new Mmochat_17_5S();
		pack.setBet(bet);
		pack.setMe(me);
		pack.setSystemMsg(bet.getBetSystemMsg());
		MmochatUtil.sendCommonPack(me, pack);

		return null;
	}

	// 请求刷新玩家压注数
	@SkymobiService
	public SkymobiProtocolMessage handleC_17_4(SkymobiHandler handler,
			Mmochat_17_4C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		MmochatBetType type = MmochatBetType.get(req.getBetType());
		if (type == null) {
			log.error("不存在的赌场类型:key=" + req.getBetType());
			return MmochatUtil.msgbox(req.getHallState(), "暂未开放!");
		}
		MmochatBet bet = bets.get(type);
		if (bet == null) {
			log.error("不存在的赌场类型:key=" + req.getBetType());
			return MmochatUtil.msgbox(req.getHallState(), "暂未开放!");
		}
		Mmochat_17_5S pack = new Mmochat_17_5S();
		pack.setBet(bet);
		pack.setMe(me);
		pack.setSystemMsg(bet.getBetSystemMsg());

		return pack;
	}

}
